<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script type="text/javascript">
    var dictsDataGrid;
    var dictsTree;

    $(function() {
        dictsTree = $('#dictsTree').tree({
        <shiro:hasPermission name="/dict/edit">
        	dnd: true,
        	animate: true,
        </shiro:hasPermission>
        	lines:true,
            url : '${path }/dict/tree',
            parentField : 'pid',
            onClick : function(node) {
            	$(this).tree('toggle', node.target);
            	$('#parentid').val(node.id);
            	$(".dict").layout('panel','center').panel('setTitle','当前栏目：'+node.text+" 》 信息列表");
                dictsDataGrid.datagrid('load', {
                    parentid: node.id
                });
            },
        onDrop: function(target, source, point){
	         var targetId = $('#dictsTree').tree('getNode', target).id;
		         $.ajax({
		            url: '${path }/dict/move',
		            type: 'post',
		            dataType: 'json',
		            data: {
		                dictcode: source.id,
		                parentid: targetId
		            },
		            success: function(result){
	                 parent.$.messager.show({title: '提示！',msg:result.msg});
	                }
		        });
		        
	        }
        });

        dictsDataGrid = $('#dictsDataGrid').datagrid({
            url : '${path }/dict/dataGrid',
            fit : true,
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'sequence',
	        sortOrder : 'asc',
            pageSize : 10,
            pageList : [ 10, 20, 30, 40, 50 ],
            columns : [ [ {
                width : '180',
                title : '编码',
                field : 'dictcode',
                sortable : true
            }, {
                width : '180',
                title : '名称',
                field : 'dictname',
                sortable : true
            },{
                width : '50',
                title : '排序',
                field : 'sequence'
            },{
                width : '60',
                title : '状态',
                field : 'status',
                sortable : true,
                formatter : function(value, row, index) {
                    switch (value) {
                    case '0':
                        return '停用';
                    case '1':
                        return '正常';
                    }
                }
            },{
                width : '120',
                title : '备注',
                field : 'remark',
                sortable : true
            } , {
                field : 'action',
                title : '操作',
                width : 130,
                formatter : function(value, row, index) {
                    var str = '';
                        <shiro:hasPermission name="/dict/edit">
                            str += $.formatString('<a href="javascript:void(0)" class="dict-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editDictsFun(\'{0}\');" >编辑</a>', row.id);
                        </shiro:hasPermission>
                        <shiro:hasPermission name="/dict/delete">
                            str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                            str += $.formatString('<a href="javascript:void(0)" class="dict-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteDictsFun(\'{0}\');" >删除</a>', row.dictcode);
                        </shiro:hasPermission>
                    return str;
                }
            }] ],
            onLoadSuccess:function(data){
                $('.dict-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.dict-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#dictsToolbar'
        });
    });
    

    function addDictsFun() {
    	var id="";
        parent.$.modalDialog({
            title : '添加信息',
            width : 500,
            height : 300,
            href : '${path }/dict/add',
            buttons : [ {
                text : '保存',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = dictsDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#dictEditForm');
                    f.submit();
                    $("#dictsTree").tree("reload");
                }
            } ]
        });
    }
    //获取父id
    function getParentId(){
    	var parentId=$("#parentid").val();
    	return parentId;
    }
    function deleteDictsFun(id) {
     if (id == undefined) {
       		var rows = dictsDataGrid.datagrid('getSelections');
        	id = rows[0].dictcode;
        } else {
            dictsDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除当前信息？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path }/dict/delete', {
                    id : id
                }, function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        dictsDataGrid.datagrid('reload');
                        $("#dictsTree").tree("reload");
                    } else {
                        parent.$.messager.alert('错误', result.msg, 'error');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
    
    function editDictsFun(id) {
        if (id == undefined) {
            var rows = dictsDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
            dictsDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.modalDialog({
            title : '编辑信息',
            width : 500,
            height : 300,
            href : '${path }/dict/edit?id=' + id,
            buttons : [ {
                text : '保存',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = dictsDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#dictEditForm');
                    f.submit();
                    $("#dictsTree").tree("reload");
                }
            } ]
        });
    }
    
    function searchDictFun() {
        dictsDataGrid.datagrid('load', $.serializeObject($('#searchDictsForm')));
    }
    function cleanDictsFun() {
        $('#searchDictsForm input').val('');
        dictsDataGrid.datagrid('load', {});
    }
</script>
<div class="easyui-layout dict" data-options="fit:true,border:false">
    <div data-options="region:'center',border:true,title:'信息列表'" >
        <table id="dictsDataGrid" data-options="fit:true,border:false"></table>
    </div>
    <div data-options="region:'west',border:true,split:false,hide:true,title:'数据字典',refresh:'reloadAll',expand:'expandAll'" collapse="collapseAll" style="width:150px; ">
        <ul id="dictsTree" fit="true" style="width:160px;margin: 10px 10px 10px 10px"></ul>
    </div>
</div>
<input type="hidden" id="parentid" value="" />
<div id="dictsToolbar" style="display: none;">
    <shiro:hasPermission name="/dict/add">
        <a onclick="addDictsFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    </shiro:hasPermission>
</div>